package com.huahang.fashionpress.service.impl;

import com.huahang.fashionpress.form.FormUserFull;
import com.huahang.fashionpress.mapper.UserMapper;
import com.huahang.fashionpress.model.User;
import com.huahang.fashionpress.service.IUSerInfoService;
import com.huahang.fashionpress.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 用户信息service
 * @author : 王翰聪
 * @since : 2019/08/22
 * @version : 1.0
 */

@Service
public class UserInfoServiceImpl implements IUSerInfoService {

    @Autowired
    UserMapper userMapper;

    /**
     * 修改已经登陆用户的信息,只能修改用户名、用户电话、用户邮箱三个字段
     * @param userID 用户登录后存储在前端session中的数据
     * @param formUserFull 用户修改后的user内容
     */
    @Override
    public void updateUser(Integer userID, FormUserFull formUserFull) {
        User user = userMapper.selectById(userID);
        //如果传入的用户名不为空
        if(formUserFull.getUserUsername() != null && !formUserFull.getUserUsername().equals("")){
            user.setUserUsername(formUserFull.getUserUsername());
        }

        //如果传入的用户电话不为空
        if(formUserFull.getPhone() != null && !formUserFull.getPhone().equals("")){
            user.setUserPhone(formUserFull.getPhone());
        }

        //如果传入的用户邮箱不为空
        if(formUserFull.getEMail() != null && !formUserFull.getEMail().equals("")){
            user.setUserEmail(formUserFull.getEMail());
        }

        //update用户
        userMapper.updateById(user);
    }

    /**
     * 获得已经登录的用户信息
     * @param userID 用户登录id
     * @return 用户VO
     */
    @Override
    public UserInfoVO getUserInfo(Integer userID) {
        //获得用户详细信息
        User user = userMapper.selectById(userID);
        //新建用户VO对象
        UserInfoVO userInfoVO = new UserInfoVO();
        //封装信息
        userInfoVO.setUsername(user.getUserUsername());
        userInfoVO.setEMail(user.getUserEmail());
        userInfoVO.setPhone(user.getUserPhone());

        return userInfoVO;
    }

    /**
     * 修改账户密码
     * @param userID 用户id
     * @param password 用户密码
     * @return 是否修改成功
     */
    @Override
    public boolean updatePassword(Integer userID, String password) {
        User user = userMapper.selectById(userID);

        user.setUserPassword(password);

        return userMapper.updateById(user) == 1;
    }

    @Override
    public boolean updateUserInfo(Integer userID, FormUserFull formUserFull) {
        User user = userMapper.selectById(userID);

        //检查字段
        if(formUserFull.getUserUsername() != null && !formUserFull.getUserUsername().isEmpty()){
            user.setUserUsername(formUserFull.getUserUsername());
        }
        if(formUserFull.getPhone() != null && !formUserFull.getPhone().isEmpty()){
            user.setUserPhone(formUserFull.getPhone());
        }
        if(formUserFull.getEMail() != null && !formUserFull.getEMail().isEmpty()){
            user.setUserEmail(formUserFull.getEMail());
        }

        //更新用户信息
        userMapper.updateById(user);

        return true;
    }
}
